package com.credithc.signature.common.utils.sequence;

/**
 * 描述：简单序号生成器<br/>
 * 作者：sunyang
 * 创建：2019/8/26 10:13
 * 版本：v1.0.0<br/>
 */
public class SimpleSequence implements ISequenceGenerater {
    private volatile long maxValue;
    private volatile long increment;
    private volatile long currentValue;
    private String name;

    public String getName() {
        return name;
    }

    public SimpleSequence(final String sequenceName, final long maxValue, final long increment) {
        this.maxValue = 0L;
        this.increment = 0L;
        this.currentValue = 0L;
        this.name = null;
        this.name = sequenceName;
        this.maxValue = maxValue;
        this.increment = increment;
    }

    @Override
    public synchronized long nextValue() {
        if (this.currentValue + this.increment <= this.maxValue) {
            this.currentValue += this.increment;
        }
        else {
            this.currentValue = 0L;
        }
        return this.currentValue;
    }
}
